<?php


namespace App\Http\Controllers\App;


use App\Http\Controllers\Controller;
use App\Models\BootPage;
use App\Models\Slide;
use App\Models\Store;
use App\Models\StoreCategory;
use DB;
use function foo\func;

class IndexController extends Controller
{
    public function slide()
    {
        return Slide::all();
    }
    public function categories()
    {
        return StoreCategory::all();
    }

    public function stores()
    {
        $category_id = request('category_id');
        $area = request('area');
        $lon = request('lon');
        $let = request('let');
        $select = ['id','name','background','area','discount', 'score','price'];
        if ($lon && $let) {
            array_push($select, DB::raw(Store::distance($lon, $let)));
        }
        $model = Store::select($select)->where('is_close', 1)->whereCategoryId($category_id);
        if (!$lon || !$let) {
            $model->where('area','like', "%$area%");
        }
        return $model->get();
    }

    public function bootPage()
    {
        $boot_page = BootPage::all();

        return ['data' => $boot_page];
    }

    public function search()
    {
        $key = request('key');

        return Store::select('id', 'name', 'logo', 'area', 'score')->where('name','like', "%$key%")->paginate();
    }
}